package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.pojo.Orders;
import com.itheima.reggie.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 订单管理
 */
@RestController
@RequestMapping("order")
public class OrdersController {

    /**
     * 添加订单表和订单项
     */
    @Autowired
    OrdersService ordersService;

    @PostMapping("submit")
    public R<String> submit(@RequestBody Orders orders){

        ordersService.submitOrer(orders);
        return R.success("添加成功");
    }
    /**
     * 查看订单
     */
    @GetMapping("userPage")
    public R<Page<OrdersDto>> userPage(Integer page,Integer pageSize){

        Long userId = BaseContext.get();
        Page<OrdersDto> ordersDtoPage=ordersService.getOrdersDtoPage(page,pageSize,userId);
        return R.success(ordersDtoPage);
    }
    /**
     * 订单页面分页查询
     */
    @GetMapping("page")
    public R<Page<Orders>> page(Integer page
            , Integer pageSize
            , Long number
            , @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime
            , @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime){
        Page<Orders> pageInto = new Page<>(page, pageSize);

        ordersService.lambdaQuery()
                .like(number!=null,Orders::getId,number)
                .between(beginTime!=null && endTime!=null,Orders::getOrderTime,beginTime,endTime)
                .page(pageInto);

        return R.success(pageInto);
    }
    @PutMapping
    public R<String> putorders(@RequestBody Orders orders){

        ordersService.lambdaUpdate()
                .set(Orders::getStatus,orders.getStatus())
                .eq(Orders::getId,orders.getId())
                .update();
        return R.success("签收成功");
    }

    //移动端再来一单
    @PostMapping("again")
    public R<String> again(@RequestBody Orders orders){

        ordersService.getshopping(orders);

        return R.success("再来一单成功");
}
}
